package debtbundler.domain.entity.impl;

import java.util.logging.Level;
import java.util.logging.Logger;

import debtbundler.domain.entity.MoneyPool;
import debtbundler.domain.entity.Transaction;

/**
 * Base class for transactions.
 * 
 * @author Anssi
 *
 */
abstract class AbstractTransactionImpl extends AbstractListenableEntity implements Transaction {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(AbstractTransactionImpl.class.getName());
	
	protected AbstractMoneyPoolImpl fromPool;
	protected AbstractMoneyPoolImpl toPool;
	protected double value = 0;
	
	protected AbstractTransactionImpl(AbstractMoneyPoolImpl fromPool, AbstractMoneyPoolImpl toPool) {
		super();
		
		if (fromPool == null || toPool == null) {
			throw new IllegalArgumentException("Null pool not allowed");
		}
		this.fromPool = fromPool;
		this.toPool = toPool;
	}

	@Override
	public MoneyPool getFromPool() {
		return fromPool;
	}

	@Override
	public MoneyPool getToPool() {
		return toPool;
	}

	@Override
	public double getValue() {
		return value;
	}

	@Override
	public void setValue(double value) throws IllegalArgumentException {
		if (value < 0) {
			throw new IllegalArgumentException("Value must be >= 0"); 
		}
		this.value = value;
		logger.log(Level.FINE, "Value updated, now {0}", this);
		fireEntityUpdateEvent();
	}
	
	@Override
	public String toString() {
		return fromPool + " -> " + toPool + ": " + value;
	}		

}
